package cn.linran._02_asynchronous;


import org.apache.commons.validator.Var;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class asynProducer {
    public static void main(String[] args) throws Exception {
        // 1.创建生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("asyn_group");
        // 2.连接NameServer
        producer.setNamesrvAddr("localhost:9876");
        //3.启动生产者
        producer.start();
        // 4.创建信息
        Message message = new Message("asyn_topic", "asyn_tags","今天新七三".getBytes(StandardCharsets.UTF_8));
        // 5.发送异步消息
        producer.send(message, new SendCallback() {
            //5.1成功后执行的函数
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }
            //5.2执行失败后执行的函数
            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败");
            }
        });
    }


}
